package code.lucamarrocco.struts2.rest.sample;


public class PersonResource extends Person {
	private IdGenerator idGenerator;

	private UnitOfWork unitOfWork;
	
	public void setUnitOfWork(UnitOfWork unitOfWork) {
		this.unitOfWork = unitOfWork;
	}
	
	public String createPerson() {
		unitOfWork.registerNew(newPerson());
		return "success";
	}

	public String deletePerson() {
		unitOfWork.registerDeleted(person());
		return "success";
	}

	public String readPerson() {
		unitOfWork.registerClean(person());
		return "success";
	}

	public String updatePerson() {
		unitOfWork.registerDirty(person());
		return "success";
	}

	private Person person() {
		return this;
	}

	private Person newPerson() {
		setId(idGenerator.newId());
		return this;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}
}